#include<iostream>
#include<string>
using namespace std;
const int N = 1e4 + 10;
int x[N], t[N], u[N];
int main()
{
	string s;
	int z;
	cin >> z;
	while (z--) {
		cin >> s;
		s = " " + s;
		int cnt = 0;
		memset(x, 0, sizeof(x));
		memset(t, 0, sizeof(t));
		memset(u, 0, sizeof(u));
		for (int i = 1; i <= s.size() - 1; i++) {
			x[i] = x[i - 1];
			t[i] = t[i - 1];
			u[i] = u[i - 1];
			if (s[i] == 'X')x[i] = x[i - 1] + 1;
			else if (s[i] == 'T')t[i] = t[i - 1] + 1;
			else u[i] = u[i - 1] + 1;
			/*cout << "x=" << x[i] << endl;
			cout << "t=" << t[i] << endl;
			cout << "u=" << u[i] << endl;*/
		}
		for (int i = 1; i <= s.size() - 1; i++) {
			for (int len = 3; len <= s.size() - 1; len += 3) {
				if (i + len - 1 <= s.size()) {
					int a = x[i + len - 1] - x[i - 1];
					int b = t[i + len - 1] - t[i - 1];
					int c = u[i + len - 1] - u[i - 1];
					/*cout << "a=" << a << endl;
					cout << "b=" << b << endl;
					cout << "c=" << c << endl;*/
					if (a == b && b == c)cnt++;
				}
			}
		}
	}
	return 0;
}